#!/bin/bash
cd ~/app-root/data/temp/firefox/
base="https://ftp.mozilla.org/pub/firefox/candidates"
getLatest(){
    sver="$1" #52.0或52.0esr或52.0b10
    ver="$sver-candidates"
    build=$(curl -s -kL $base/$ver/|grep -o "build[1-9]"|sort -u|tail -1)
    if echo $ver |grep -q esr; then
        channel="ESR"
        oldver=$(cat oldesr)
    elif echo $ver |grep -q b; then
        channel="Beta"
        oldver=$(cat oldbeta)
    else
        channel="Release"
        oldver=$(cat oldre)
    fi
    if ! grep -q $ver/$build/mac ~/app-root/data/index/firefox.html; then
        fname=$(curl -s -kL $base/$ver/$build/mac/zh-CN/|grep -o "Firefox[a-zA-Z0-9\.\ _-]*\.dmg"|sort -u)
        if [ -n "$fname" ]; then
            sed -i -e "20,45s#".*"${oldver}".*"##" -e '/^$/d' ~/app-root/data/index/firefox.html
            if echo $ver |grep -q esr; then
                echo "$ver/$build" > ./oldesr
            elif echo $ver |grep -q b; then
                echo "$ver/$build" > ./oldbeta
            else
                echo "$ver/$build" > ./oldre
            fi
            sed -i "/$channel/c\ <tr><th>$channel $sver 【$build】</th>\n  <td class=\"download mac\"><a href=\"$base/$ver/$build/mac/en-US/$fname\">英文版</a></td><td><a href=\"$base/$ver/$build/mac/zh-CN/$fname\">简中版</a></td>" ~/app-root/data/index/firefox.html
        fi
    fi
    if ! grep -q $ver/$build/linux-i686 ~/app-root/data/index/firefox.html; then
        fname=$(curl -s -kL $base/$ver/$build/linux-i686/zh-CN/|grep -o "firefox[a-zA-Z0-9\.\ _-]*\.bz2"|sort -u)
        if [ -n "$fname" ]; then
            sed -i "/$ver\/$build\/mac/a\  <td class=\"download linux\"><a href=\"$base/$ver/$build/linux-i686/en-US/$fname\">英文版</a></td><td><a href=\"$base/$ver/$build/linux-i686/zh-CN/$fname\">简中版</a></td>" ~/app-root/data/index/firefox.html
        fi
    fi
    if ! grep -q $ver/$build/linux-x86_64 ~/app-root/data/index/firefox.html; then
        fname=$(curl -s -kL $base/$ver/$build/linux-x86_64/zh-CN/|grep -o "firefox[a-zA-Z0-9\.\ _-]*\.bz2"|sort -u)
        if [ -n "$fname" ]; then
            sed -i "/$ver\/$build\/linux-i686/a\  <td class=\"download linux64\"><a href=\"$base/$ver/$build/linux-x86_64/en-US/$fname\">英文版</a></td><td><a href=\"$base/$ver/$build/linux-x86_64/zh-CN/$fname\">简中版</a></td></tr>" ~/app-root/data/index/firefox.html
        fi
    fi
    if ! grep -q $ver/$build/win64 ~/app-root/data/index/firefox.html; then
        fname=$(curl -s -kL $base/$ver/$build/win64/zh-CN/|grep -o "Firefox Setup $sver\.exe"|sort -u)
        if [ -n "$fname" ]; then
            sed -i "/$ver\/$build\/mac/i\  <td class=\"download win64\"><a href=\"$base/$ver/$build/win64/en-US/$fname\">英文版</a></td><td><a href=\"$base/$ver/$build/win64/zh-CN/$fname\">简中版</a></td>" ~/app-root/data/index/firefox.html
        fi
    fi
    if ! grep -q $ver/$build/win32 ~/app-root/data/index/firefox.html; then
        fname=$(curl -s -kL $base/$ver/$build/win32/zh-CN/|grep -o "Firefox Setup $sver\.exe"|sort -u)
        if [ -n "$fname" ]; then
            sed -i "/$sver 【$build】/a\  <td class=\"download win\"><a href=\"$base/$ver/$build/win32/en-US/$fname\">英文版</a></td><td><a href=\"$base/$ver/$build/win32/zh-CN/$fname\">简中版</a></td>" ~/app-root/data/index/firefox.html
        fi
    fi
}

curl -s -kL $base/ -o firefox.html
cp -f ~/app-root/data/index/firefox.html ~/app-root/data/temp/firefox/firefox1.html
#esr
getLatest "$(grep -o "[0-9]\{2\}\.[0-9\.]*[0-9]\{1\}esr-candidates" firefox.html|sort -u|grep -o '[0-9\.]*'|sort -t.|tail -1)esr"
#beta
latest=$(grep -o "[0-9]\{2\}\.[0-9\.]*[0-9]\{1\}b[0-9]\{1,2\}-candidates" firefox.html|grep -o '^[0-9]\{2\}'|tail -1)
getLatest "$(grep -o "[0-9]\{2\}\.[0-9\.]*[0-9]\{1\}b[0-9]\{1,2\}-candidates" firefox.html|sort -u|grep -o "$latest[0-9b\.]*"|tail -11|sort -t 'b' -k2n|tail -1)"
#release
getLatest "$(grep -o "[0-9]\{2\}\.[0-9\.]*[0-9]\{1\}-candidates" firefox.html|grep -o '[0-9\.]*'|tail -1)"

if ! diff -q ~/app-root/data/index/firefox.html ~/app-root/data/temp/firefox/firefox1.html; then
    sh ~/app-root/data/github-sh/firefox/time.sh
    cd ~/app-root/data/index/
    git add --all
    git commit -m "update"
    git push -u origin gh-pages
fi
rm -f ~/app-root/data/temp/firefox/firefox.html ~/app-root/data/temp/firefox/firefox1.html
